package com.neuedu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.common.ServerResponse;
import com.neuedu.entity.Dept;
import com.neuedu.entity.Doctor;
import com.neuedu.service.DeptService;
import com.neuedu.service.DoctorService;
import com.neuedu.util.StringUtil;
import com.neuedu.vo.DoctorCondition;
import com.neuedu.vo.DoctorRegCondition;
import com.neuedu.vo.DoctorVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Api("医生管理")
@RestController
@RequestMapping("/doctor")
@CrossOrigin
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private DeptService deptService;


    @ApiOperation("医生列表")
    @GetMapping("/list")
    public ServerResponse list(DoctorCondition doctorCondition) {

        System.out.println(doctorCondition);

        //调用业务方法
        Page<DoctorVO> doctorVOPage = doctorService.getDoctorList(doctorCondition);

        //返回分页对象
        return ServerResponse.success("").data("doctorVOPage", doctorVOPage);
    }

    @ApiOperation("添加医生")
    @PostMapping("/add")
    public ServerResponse add(@RequestBody Doctor doctor) {

        System.out.println(doctor);

        //服务器端数据校验
        if (StringUtils.isEmpty(doctor.getDoctorName())) {
            return ServerResponse.error("医生名称不能为空");
        }

        if (doctor.getDoctorName().length() > 10) {
            return ServerResponse.error("医生名称的长度不能超过10个字符");
        }

        if (doctorService.save(doctor)) {
            return ServerResponse.success("添加医生成功");
        } else {
            return ServerResponse.error("添加医生失败");
        }
    }

    @ApiOperation("根据编号获取医生")
    @GetMapping("/get/{doctorId}")
    public ServerResponse get(@PathVariable int doctorId) {

        Doctor doctor = doctorService.getById(doctorId);

        if (doctor != null) {
            return ServerResponse.success("").data("doctor", doctor);
        } else {
            return ServerResponse.error("获取医生信息失败");
        }
    }

    @ApiOperation("修改医生")
    @PutMapping("/modify")
    public ServerResponse modify(@RequestBody Doctor doctor) {

        System.out.println(doctor);

        //服务器端数据校验
        if (StringUtils.isEmpty(doctor.getDoctorName())) {
            return ServerResponse.error("医生名称不能为空");
        }

        if (doctor.getDoctorName().length() > 10) {
            return ServerResponse.error("医生名称的长度不能超过10个字符");
        }

        if (doctorService.updateById(doctor)) {
            return ServerResponse.success("修改医生成功");
        } else {
            return ServerResponse.error("修改医生失败");
        }
    }

    @ApiOperation("删除医生")
    @DeleteMapping("/remove/{doctorId}")
    public ServerResponse remove(@PathVariable int doctorId) {

        if (doctorService.removeById(doctorId)) {
            return ServerResponse.success("删除医生成功");
        } else {
            return ServerResponse.error("删除医生失败");
        }
    }

    @ApiOperation("判断指定的医生是否可以删除")
    @GetMapping("/removeEnable/{doctorId}")
    public ServerResponse removeEnable(@PathVariable int doctorId) {

        if (doctorService.removeEnable(doctorId)) {
            return ServerResponse.success("允许删除医生");
        } else {
            return ServerResponse.error("当前医生下存在处方，不能删除");
        }
    }

    @ApiOperation("批量删除医生")
    @DeleteMapping("/deleteBatch")
    public ServerResponse deleteBatch(String doctorIds) {

        if (doctorService.deleteBatch(doctorIds)) {
            return ServerResponse.success("批量删除医生成功");
        } else {
            return ServerResponse.error("批量删除医生成功失败");
        }
    }

    @ApiOperation("上传医生照片")
    @PostMapping("/upload")
    public ServerResponse upload(MultipartFile file) {


        //获取原始的文件名
        String originalFilename = file.getOriginalFilename();

        //获取文件的扩展名
        String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

        //文件改名
        String serverFilename = StringUtil.generateFilename() + "." + extension;
        System.out.println(serverFilename);

        //获取服务器上的保存文件的路径
        String serverFilePath = Thread.currentThread().getContextClassLoader().getResource("static/image/photo").getPath();
        System.out.println(serverFilePath);

        //创建目标文件对象
        File dest = new File(serverFilePath + "/" + serverFilename);

        try {
            //传输文件数据
            file.transferTo(dest);

            //返回新的文件名
            return ServerResponse.success("医生照片上传成功").data("serverFilename", serverFilename);

        } catch (IOException e) {
            e.printStackTrace();
            return ServerResponse.error("医生照片上传失败");
        }
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ServerResponse login(@RequestBody Doctor doctor, HttpSession session) {

        System.out.println(doctor);

        doctor = doctorService.login(doctor);

        System.out.println(doctor);

        if (doctor != null) {  //找到匹配的记录

            //在session属性范围中保存用户对象
            session.setAttribute("currentUser", doctor);


            //转换医生的类型名称
            String doctorType = "";
            if (doctor.getDoctorType() == 1) {
                doctorType = "门诊医生";
            } else if (doctor.getDoctorType() == 2) {
                doctorType = "检查医生";
            } else if (doctor.getDoctorType() == 3) {
                doctorType = "挂号收费";
            } else {
                doctorType = "管理员";
            }

            System.out.println(doctor.getDoctorType());
            //转换医生的科室名称
            String deptName = "";
            Dept dept = deptService.getById(doctor.getDeptId());

            //如果用户有科室(门诊医生或检查医生)，才获取科室名称
            if (dept != null) {
                deptName = dept.getDeptName();
            }

            //返回
            return ServerResponse.success("登录成功")
                    .data("currentUser", doctor)
                    .data("doctorType", doctorType)
                    .data("deptName", deptName);
        } else {
            return ServerResponse.error("登录失败，用户名或密码错误");
        }

    }

    @ApiOperation("用户注销")
    @PostMapping("/logout")
    public ServerResponse logout(HttpSession session) {

        //从session属性范围中删除用户对象
        session.removeAttribute("currentUser");

        return ServerResponse.success("注销成功");
    }

    @ApiOperation("医生列表---挂号时使用")
    @GetMapping("/listReg")
    public ServerResponse list(DoctorRegCondition doctorRegCondition) {

        System.out.println(doctorRegCondition.toString());
        List<Doctor> doctorList = doctorService.getDoctorList(doctorRegCondition);

        return ServerResponse.success("").data("doctorList", doctorList);
    }

}